<template>
  <div>
    <h2 class="mb-3 text-2xl font-bold text-center xl:text-3xl enter-x xl:text-left">
      登录
    </h2>
  </div>
  <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef">
    <FormItem name="loginName" class="enter-x">
      <Input size="large" v-model:value="formData.loginName" :placeholder="t('sys.login.userName')"
        class="fix-auto-fill" />
    </FormItem>
    <FormItem name="password" class="enter-x">
      <InputPassword size="large" visibilityToggle v-model:value="formData.password"
        :placeholder="t('sys.login.password')" />
    </FormItem>
    <FormItem name="captcha" class="enter-x">
      <a-input v-model:value="formData.captcha" size="large" :placeholder="t('sys.login.captcha')" class="fix-auto-fill">
        <template #addonAfter>
          <Image @click.stop="getCaptchaImg" :src="formData.captcharSrc" :width="80" :preview="false" />
        </template>
      </a-input>
    </FormItem>
    <ARow class="enter-x">
      <ACol :span="12">
        <FormItem>
          <Checkbox v-model:checked="rememberMe" size="small">
            {{ t('sys.login.rememberMe') }}
          </Checkbox>
        </FormItem>
      </ACol>
    </ARow>
    <FormItem class="enter-x">
      <Button type="primary" size="large" block @click="handleLogin" :loading="loading">
        {{ t('sys.login.loginButton') }}
      </Button>
    </FormItem>
  </Form>
</template>
<script lang="ts" setup>
import { Button, Checkbox, Col, Form, Image, Input, Row } from 'ant-design-vue';
import { reactive, ref } from 'vue';
import { useFormRules, useFormValid } from './useLogin';
import { getCaptcha } from '/@/api/sys/auth';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { useUserStore } from '/@/store/modules/user';

const ACol = Col;
const ARow = Row;
const FormItem = Form.Item;
const InputPassword = Input.Password;
const { t } = useI18n();
const { notification } = useMessage();
const userStore = useUserStore();

const { getFormRules } = useFormRules();

const formRef = ref();
const loading = ref(false);
const rememberMe = ref(false);

const formData = reactive({
  loginName: 'admin',
  password: '111111',
  captcha: '',
  code: '',
  captcharSrc: '',
});

getCaptchaImg();

const { validForm } = useFormValid(formRef);

async function getCaptchaImg() {
  let result = await getCaptcha();
  formData.captcha = '';
  formData.code = result.data.code;
  formData.captcharSrc = result.data.captcha;
}

async function handleLogin() {
  const data = await validForm();
  if (!data) return;
  try {
    loading.value = true;
    let param: any = {
      password: data.password,
      loginName: data.loginName,
      mode: 'message',
    };
    param.captcha = data.captcha;
    param.code = formData.code;
    const userInfo = await userStore.login(param);
    if (userInfo) {
      notification.success({
        message: t('sys.login.loginSuccessTitle'),
        description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.loginName}`,
        duration: 3,
      });
    }
  } catch (error) {
    getCaptchaImg();
  } finally {
    loading.value = false;
  }
}
</script>
